package com.test.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.test.ssm.pojo.AjaxMessage;
import com.test.ssm.pojo.Role;
import com.test.ssm.pojo.TableData;
import com.test.ssm.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("role.html")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @RequestMapping
    public String role() {
        return "role";
    }

    @RequestMapping(params = "act=table")
    @ResponseBody
    public TableData list(Role role, Integer page, Integer limit) {
        PageInfo<Role> pageInfo = roleService.getRoleList(role, page, limit);
        return new TableData(pageInfo.getTotal(), pageInfo.getList());
    }

    @RequestMapping(params = "act=go_edit")
    @ResponseBody
    public Role goEdit(Integer id) {
        return roleService.getRoleById(id);
    }

    @RequestMapping(params = "act=edit")
    @ResponseBody
    public AjaxMessage edit(Role role) {
        if (role.getId() == null) {
            roleService.addRole(role);
        } else {
            roleService.updateRole(role);
        }
        return new AjaxMessage(true, "成功");
    }

    @RequestMapping(params = "act=delete")
    @ResponseBody
    public AjaxMessage delete(Integer[] ids){
        roleService.deleteRole(ids);
        return new AjaxMessage(true, "成功");
    }
}

